package backend


import(
	/*
	"encoding/json"
	"log"
	*/
	"fmt"
	"gopkg.in/mgo.v2/bson"
	"time"
)


//Template of questions. Questions devided into sections, each with array of questions
//Question refers to a list of template answers 
type Template struct{
	Id bson.ObjectId `bson:"_id"`
	Version int8
	//Name and Description of this template. Useful later when we've many templates
	Name , Desc, UID string
	Sections []Section
}
//Section groups a set of questions that have the same criteria such as Business, Technical, etc...
type Section struct{
	Name, Desc string
	Questions []TQ
}

//Area is used as a question marker to be used later in generating reports of that area. Example is 
//get all questions that relates to financial or business factors.
type Area struct{

}

//Template Question
type TQ struct{
	//Text, Desc string
	Text, Desc ,Type string
	//Area: related area of this question
	Id, Area int8
	Multi, Required bool
	Answers []TA
	Defaults []int8
}
//Template Answer
type TA struct{
	//Id, Weight, Max and Min length for string or max number for ditits
	Id, Wght, Max, Min int8
	//Text, Description, Length [short, medium, long], Type[string, digit, float, date]
	Text, Desc, Length, Tip, DataType string
	Required, WithText bool
}


//type JsonCommand struct{ Cmd string ; Data map[string]interface{}}
type JsonCommand struct{ Cmd string ; Data map[string]interface{}
Org *Org; User *User;

}
type JsonRes struct{ Msg string ; Error string;  Data interface{}}

func (cmd *JsonCommand) String() string{
	fmt.Println(*cmd)
	return fmt.Sprintf("Blog Command: %s", cmd.Cmd)
}

////////////////////////////////////////////////////////
type Comment struct{
	Text string
	UserCreated bson.ObjectId
	Show bool
	Like, Dislike int8
	Created time.Time
}


//Answers of a Template per organization. Holds organization and update details
type TemplateAnswers struct{
	Id  bson.ObjectId `bson:"_id"`
	UID string //UID of original template that this model answers
	Version int8 //incremented with each update
	OrgId  bson.ObjectId
	UserCreatedId, UserUpdatedId bson.ObjectId
	Created, Updated time.Time
	Questions map[int8]Question
}



// Question = {Single:null, Choices:{}, Wght:qTpl.Wght}
// Choices = [] of answer {Wght: answerTpl.Wght, Text:null, Checked:false}
type Question struct{
	Single , Wght int8 // TA Id of single choice from radio or select component
	Choices []Answer // maps to array of TA above
}

//Holds final selection or text entered by user
type Answer struct{
	Wght int8
	Text string
	Checked bool 
}


// Save answered template to DB
func (t *TemplateAnswers) Save() error {
	//TODO get current user and org ID

	s, c := GetColl(nil, "answers")
	defer s.Close()

	t.Version = t.Version +1

	if t.Id == ""{
		t.Id = bson.NewObjectId()
		t.Created = time.Now()
		//TODO t.UserCreated = 
		return c.Insert(t)
	}
	t.Updated = time.Now()
	//TODO t.UserUpdated = 
	return c.UpdateId(t.Id, t)

}


//////////////////////

type User struct{
	Id string `bson:"_id"`
	OrgId string
	Name, Email, Phone1, Phone2, OrgEmail string
	Password string
	Roles []string
}

func (u *User) NewId(){
	u.Id= bson.NewObjectId().Hex()
}


///////////////////////////////

type Org struct{
	//Id bson.ObjectId `bson:"_id"`
	Id string `bson:"_id"`
	Name, Email,  Url, Address, UserEmail, Password string
	Created time.Time //Construction Date
	Product, Country string
	Templates map[string]int8 //templates to be answerd as map[UID]Version of the template
	Answers []TemplateAnswers // array of answered templates
	UsersIds []bson.ObjectId // Org users
	
}

